package by.kniazhych.core.controllers {
	
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	
	import by.kniazhych.core.data.game.GameData;
	import by.kniazhych.core.view.frames.BaseFrameView;
	import by.kniazhych.core.view.frames.GameFrameView;
	import by.kniazhych.core.view.frames.MenuFrame;
	import by.vaukalak.core.commands.Command;
	import by.vaukalak.core.commands.CommandDispatcher;
	import by.vaukalak.core.commands.ICommandDispatcher;
	import by.vaukalak.core.controllers.IBaseController;
	import by.vaukalak.core.data.DataContainer;
	import flash.errors.IllegalOperationError;
	

	
	public class NativeGameController extends BaseFrameController implements ICommandDispatcher{
		
		private var _dispatcher:ICommandDispatcher;
		internal var $gameData:GameData;
		
		
		public function NativeGameController(baseController:IBaseController) {
			super(baseController);
			if (!(this is GameController)) throw new IllegalOperationError();
			_dispatcher = new CommandDispatcher();
		}
		
		//--------------------------------------------------------------------------
		//
		//  PROTECTED METHODS
		//
		//--------------------------------------------------------------------------
		
		internal function get $gameContainer():GameFrameView {
			return super._frame as GameFrameView;
		}
		
		//--------------------------------------------------------------------------
		//
		//  OVERRIDEN METHODS
		//
		//--------------------------------------------------------------------------
		
		override public function constuctFrame(frame:BaseFrameView):void {
			
			if (!(frame is GameFrameView)) throw new IllegalOperationError();
			super.constuctFrame(frame);
			$gameData = new GameData();
		}
		
		override public function enterFrame():void {
			super.enterFrame();
			dataBase.addChild($gameData);
		}
		
		override public function exitFrame():void {
			super.exitFrame();
			dataBase.removeChild($gameData);
		}
		
		/* INTERFACE by.vaukalak.core.commands.ICommandDispatcher */
		
		public function dispatchCommand(command:Command):void{
			_dispatcher.dispatchCommand(command);
		}
		
		public function addCommandListener(commandName:String, listener:Function, priority:int = 0, useWeakReference:Boolean = false):void{
			_dispatcher.addCommandListener.apply(null, arguments);
		}
		
		public function removeCommandListener(commandName:String, listener:Function):void{
			_dispatcher.removeCommandListener(commandName, listener);
		}
		
		public function hasCommandListener(commandName:String):Boolean{
			return _dispatcher.hasCommandListener(commandName);
		}
		
	}

}